package com.wcy.controller;

import com.gaofei.domain.FileResult;
import com.gaofei.service.FileService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wcy.bean.Shanghu;
import com.wcy.service.ShanghuService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Id;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

//定义返回类型
@RestController
//解决跨域问题
@CrossOrigin
public class ShanghuController {

    @Autowired
    private ShanghuService service;

    @Autowired
    private FileService fileService;

    @RequestMapping("/list")
    public PageInfo<Shanghu> selectList(Shanghu shanghu,
                                    @RequestParam(defaultValue = "1")Integer pageNum,
                                    @RequestParam(defaultValue = "3")Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Shanghu> list = service.selectList(shanghu);
        PageInfo<Shanghu> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    //删除
    @RequestMapping("/delById")
    public Boolean deleteById(@RequestParam("id") Integer id){
        int i = service.deleteById(id);
        if(i>0){
            return true;
        }
        return false;
    }

    //添加和修改
    @RequestMapping("insertOrUpdate")
    public Boolean insertSh(@RequestBody Shanghu shanghu){
        if(shanghu.getId()!=null){
            int i = service.UpdateById(shanghu);
            if(i>0){
                return true;
            }
            return false;
        }else{
            int i = service.insertSh(/*name, boss, unit, createTime, address*/ shanghu);
            if(i>0){
                return true;
            }
            return false;
        }

    }

    //批量删除
    @RequestMapping("deleteByIds")
    public Boolean deleteByIds(@RequestParam("ids")Integer[] ids){
        int i = service.deleteByIds(ids);
        if(i>0){
            return true;
        }
        return false;
    }

    //修改的回显
    @RequestMapping("getShById")
    public Shanghu getShById(@RequestParam("id")Integer id){
        Shanghu shanghu = service.getShById(id);
        return shanghu;
    }

    //上传图片
    @RequestMapping("uploadImage")
    public Object uploadImage(MultipartFile[] file){
        List<FileResult> upload = fileService.upload(file);
        return upload;
    }
}
